package com.vf.cloud.paas.app.master.api.scene.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.scene.service.ISceneLayer;
import com.vf.cloud.paas.app.master.common.domain.SceneLayer;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneTree;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneTreeParam;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/scene/layer")
public class SceneLayerController {

	@Autowired
	private ISceneLayer sceneLayerImpl;

	@RequestMapping(value = "/tree", method = RequestMethod.POST)
	public R<List<SceneTree>> tree(@RequestBody SceneTreeParam param) {
		return R.ok(sceneLayerImpl.getTree(param));
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<SceneLayer>> list(@RequestBody SceneTreeParam param) {
		List<SceneLayer> sceneLayers = SceneLayer.dao.find("select * from master_scene_layer order by sort asc ");
		return R.ok(sceneLayers);
	}

	@RequestMapping(value = "/addLayer", method = RequestMethod.POST)
	public R<List<SceneTree>> addLayer(@RequestBody SceneTreeParam param) {
		SceneLayer sceneLayer = new SceneLayer();
		sceneLayer.setId(UuidUtil.getUUID());
		sceneLayer.setLabel("新增文件夹");
		sceneLayer.setPid(StrKit.isBlank(param.getPid()) ? "-1" : param.getPid());
		sceneLayer.save();
		return R.ok(sceneLayerImpl.getTree(param));
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public R<SceneLayer> update(@RequestBody SceneLayer param) {
		if (param == null) {
			return R.failed("参数必填.");
		}

		SceneLayer sceneLayer = SceneLayer.dao.findById(param.getId());
		if (sceneLayer == null) {
			return R.failed("无效参数.");
		}
		if (!StrKit.isBlank(param.getLabel())) sceneLayer.setLabel(param.getLabel());

		if (!StrKit.isBlank(param.getPid())) {
			sceneLayer.setPid(param.getPid());
		}

		if (!sceneLayer.update()) {
			return R.failed("更新失败.");
		}
		return R.ok(sceneLayer);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<SceneLayer> delete(@RequestBody SceneLayer param) {
		if (param == null) {
			return R.failed("参数必填.");
		}

		SceneLayer sceneLayer = SceneLayer.dao.findById(param.getId());
		if (sceneLayer == null) {
			return R.failed("无效参数.");
		}
		sceneLayerImpl.delete(sceneLayer.getId());
		return R.ok(sceneLayer);
	}

}
